package com.unicom.projt.fee;

import android.content.Intent;
import com.unicom.projt.http.AbstractHttpController;
import com.unicom.projt.http.UriConstants;
import com.unicom.projt.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 12-5-15
 * Time: 下午8:54
 */
public class PaymentInputPhoneNumberController extends AbstractHttpController<String> {
    private PaymentInputPhoneNumberPage page;
    private static final String CHECK_NUMBER = "CHECK_NUMBER";
    private static final String CHECK_BIND = "CHECK_BIND";

    private String payPhoneNumber;
    private String actionType;

    public String getPayPhoneNumber() {
        return payPhoneNumber;
    }

    public void setPayPhoneNumber(String payPhoneNumber) {
        this.payPhoneNumber = payPhoneNumber;
    }

    public PaymentInputPhoneNumberController(PaymentInputPhoneNumberPage page) {
        super(String.class);
        this.page = page;
    }

    @Override
    public void onNetworkAbort() {
        page.hideProgressDialog();
        ToastUtils.showMessage(page, "错误，网络连接被中断.");
    }

    @Override
    public void onNetworkError() {
        page.hideProgressDialog();
        ToastUtils.showMessage(page, "错误，网络连接错误.");
    }

    public void checkThePaymentPhoneNumber(String amountPhoneNumber) {
        this.actionType = CHECK_NUMBER;
        String checkPhoneNumberXml = new PostXmlBuilder().setBiPCode(RequestCode.VERIFY_PHONE_NUMBER).setTelNo(amountPhoneNumber).createXml();
        this.sendRequest(UriConstants.URL, checkPhoneNumberXml);
    }

    public void checkIsIVRCard(String phoneNumber) {
        this.payPhoneNumber = phoneNumber;
        this.actionType = CHECK_BIND;
        String postXml = new PostXmlBuilder().setBiPCode(RequestCode.QUERY_PHONE_BIND).setTelNo(phoneNumber).createXml();
        this.sendRequest(UriConstants.URL, postXml);
    }

    @Override
    public void onResponseError() {
        page.hideProgressDialog();
        ToastUtils.showMessage(page, "错误，返回结果错误.");
    }

    @Override
    public void onRequestFinish(String response) {
        if (CHECK_NUMBER.equals(actionType)) {
            if (response == null || "".equals(response)) {
                AlertDialogUtils.showConfirmDiaLog(page, response);
                ToastUtils.showMessage(page, "错误，请稍候再试");
            } else {
                String respCode = SimpleXmlParser.getResponseCode(response);
                if (ResponseCode.NOT_UNICOM_NUMBER.equals(respCode)) {
                    page.hideProgressDialog();
                    ToastUtils.showMessage(page, "充值失败,充值号码不是联通号码");
                } else {
                    this.checkIsIVRCard(UserHolder.phoneNumber);
                }
            }
        } else {
            Intent intent = new Intent(page, PaymentPage.class);
            page.hideProgressDialog();
            if (response == null || "".equals(response)) {
                UserHolder.isBindUser = false;
            } else {
                String respCode = SimpleXmlParser.getResponseCode(response);
                if (!ResponseCode.NOT_BIND_USER.equals(respCode) && !ResponseCode.UNKNOWN_ERROR.equals(respCode)) {//绑定用户
                    UserHolder.isBindUser = true;
                    String bankName = SimpleXmlParser.getElementsValue(response, "BankName").get(0);
                    String cardNumber = SimpleXmlParser.getElementsValue(response, "BankCard").get(0);
                    UserHolder.bankName = bankName;
                    UserHolder.cardNumber = cardNumber;
                } else {
                    UserHolder.isBindUser = false;
                }
            }


            intent.putExtra("phoneNumber", this.payPhoneNumber);
            page.startActivity(intent);
            page.finish();
        }
    }

}
